With the iPython notebook and pylab support, we have a very nice interactive shell for scientific computing.
The ipython notebook lets you mix rich text including equations, computation, and graphics into a single working document. You can easily revise computational steps without having to re-run an entire script. Notebooks can be saved and reloaded, printed, and accessed remotely.
Example equation: $X = \sum_{i=1}^{n}i^2$ produces: $X = \sum_{i=1}^{n}i^2$
Pylab is the name given to a common collection of python scientific computation and plotting libraries, imported into a common namespace to be more user friendly when used interactively. Pylab imports numpy and matplotlib, a MATLAB-like plotting library.
Invoked via the command line as: ipython notebook --pylab inline
Rich text is provided using the "markdown" syntax. For information on markdown, see http://en.wikipedia.org/wiki/Markdown.
In [1]:
A = zeros((4,4))
I = eye(4)
print "A=",A, "\nI=", I
When using pylab, many common MATLAB-like functions exist in our default namespace. A good resource for those coming to python from MATLAB is http://www.scipy.org/NumPy_for_Matlab_Users.
In [2]:
N = randn(4,4)
N
Out[2]:
Note the data type is a numpy multidimensional array. </br> Addition and scalar multiplication on arrays work as expected.
In [3]:
N+4*I
Out[3]:
The * operator between arrays performs component-wise multiplication.
For matrix multiplication, use dot(X,Y) instead.
In [4]:
print N*I #array component mult.
print
print dot(N,I) #matrix mult. using arrays
There is also a matrix data type. </br> The multiplication operator * performs matrix multiplication, and the multiply(X,Y) performs component-wise multiplication.
In [5]:
N2 = matrix(N)
I2 = matrix(I)
print N2 * I2 #matrix mult
print
print multiply(N2,I2) #component mult
With PyLab, we can easily call the scipy linear algebra functions normally found in scipy.linalg package.</br> (See: http://docs.scipy.org/doc/scipy/reference/linalg.html)
In [6]:
(evs, evecs) = eig(N) #if input is ndarray, output is too.
(evs2, evecs2) = eig(N2) #if input is matrix, output is matrix
print type(evecs), type(evecs2)
In [7]:
#if not using pylab, do the following
import scipy.linalg as LA
(evs, evecs) = LA.eig(N)
We can do some easy MATLAB-like plotting too!
In [8]:
X = randn(1000)
hist(X)
Out[8]:
In [9]:
Y = 3*X
plot(X,Y)
Out[9]:
In [9]: